if name == 'main':
在 python 中常常看到下列寫法,是用來判斷該檔案是直接被執行或是被當成 module import 的
if __name__ == '__main__':
...
...
&http://technology-sea.blogspot.com/2012/03/python-name-import.html
數字類型
運算子
Built-in function
符號
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she said.
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s) # with print(), \n produces a new line
First line.
Second line.
特殊字元
原始字串符r'C:\some\name'
運算子
"abc" + "def"
"abc"*3 + "def"
支援index取值
s = 'abcdefghijklmn'
s[2] #第三個字元
s[-1] #倒數第一個字元
Slice [開始:結束:step]
s = 'abcdefghijklmn'
s[0:6:2] #[開始:結束:step]
string format
print(name + " is " + age + " year's old.") 改寫為 string format
三種寫法:
"%s..."%(變數)
name = "Jack"
age = "17"
"%s is %s year's old."%(name, age)
"{}...".format(變數)
name = "Jack"
age = "17"
"{} is {} year's old.".format(name, age)
f"{變數}..."
name = "Jack"
age = "17"
message = f"{name} is {age} year's old."
Built-in functionlen(s)
dir(s)
string method
更多字串的方法可以參考這裡